package me.aartikov.flowsample.slice;


import me.aartikov.alligator.util.Log;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.flowsample.ResourceTable;
import me.aartikov.flowsample.SampleApplication;
import me.aartikov.flowsample.screens.TestFlowScreen;
import me.aartikov.flowsample.screens.TestSmallScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.List;


@RegisterScreen(TestFlowScreen.class)
public class TestFlowMiFraction extends MiFraction {
//    Component mRootView;

    Text mCounterTextView;

    Button mForwardButton;

    Button mReplaceButton;

    Button mResetButton;

    Button mFinishButton;

    Button mFinishAllButton;


    private Navigator mNavigator = SampleApplication.getNavigator();


    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Log.i("TestFlowFragment.onComponentAttached([scatter, container, intent]):51");
        Component component = scatter.parse(ResourceTable.Layout_mifraction_flow, container, false);
        initComponent(component);
        TestFlowScreen screen = SampleApplication.getScreenResolver().getScreen(this);
        int counter = screen.getCounter();

        mCounterTextView.setText(getFractionAbility().getString(ResourceTable.String_counter_template, counter));
        mForwardButton.setClickedListener(v -> mNavigator.goForward(new TestFlowScreen(counter + 1, ResourceTable.Id_flow_fragment_container, getFractionAbility().toString())));
        mReplaceButton.setClickedListener(v -> mNavigator.replace(new TestFlowScreen(counter, ResourceTable.Id_flow_fragment_container, getFractionAbility().toString())));
        mResetButton.setClickedListener(v -> mNavigator.reset(new TestFlowScreen(1, ResourceTable.Id_flow_fragment_container, getFractionAbility().toString())));
        mFinishButton.setClickedListener(v -> mNavigator.goBack(screen, null));
        mFinishAllButton.setClickedListener(v -> mNavigator.finishTopLevel());

        return component;
    }

    public void initComponent(Component component) {
        mCounterTextView = (Text) component.findComponentById(ResourceTable.Id_counter_text_view);
        mForwardButton = (Button) component.findComponentById(ResourceTable.Id_forward_button);
        mReplaceButton = (Button) component.findComponentById(ResourceTable.Id_replace_button);
        mResetButton = (Button) component.findComponentById(ResourceTable.Id_reset_button);
        mFinishButton = (Button) component.findComponentById(ResourceTable.Id_finish_button);
        mFinishAllButton = (Button) component.findComponentById(ResourceTable.Id_finish_all_button);
//        mRootView = component.findComponentById(ResourceTable.Id_root_view);
    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }


    @Override
    public void setInitialFragmentIfRequired() {
        List<Screen> screens = FractionStack.getInstance(getFractionAbility().getFractionManager()).getCurrentFragmentScreen(this.toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            mNavigator.goForward(new TestSmallScreen(1, ResourceTable.Id_fragment_container, this.toString()));
        } else {
            mNavigator.reduce(screens);
        }
    }
}
